package com.jblv.frame.common.es.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @author happyonion.yangtao
 * @version 1.0
 * @title ESUtils
 * @projectName jblv-frame
 * @packageName com.jblv.common.es.util
 * @description ESUtils
 * @date Create at 2020/4/29 17:33
 * @since jdk1.8
 */

public class ESUtils {
    public static final String SPLIT_STR = "@!@";
    private static String KEYWORD_SUFFIX = ".keyword";

    public ESUtils() {
    }

    public static String warpKeyword(String keyword) {
        return StringUtils.isEmpty(keyword) ? null : keyword + KEYWORD_SUFFIX;
    }

    public static String warpWildcard(String keyword) {
        return StringUtils.isEmpty(keyword) ? null : "*" + keyword + "*";
    }

    public static String escapeQueryChars(String s) {
        StringBuilder sb = new StringBuilder();

        for(int i = 0; i < s.length(); ++i) {
            char c = s.charAt(i);
            if (c == '\\' || c == '+' || c == '-' || c == '!' || c == '(' || c == ')' || c == ':' || c == '^' || c == '[' || c == ']' || c == '"' || c == '{' || c == '}' || c == '~' || c == '*' || c == '?' || c == '|' || c == '&' || c == ';' || c == '/' || Character.isWhitespace(c)) {
                sb.append('\\');
            }

            sb.append(c);
        }

        return sb.toString();
    }
}

